package com.jfeat.am.module.wegou.api.crud;

import com.baomidou.mybatisplus.plugins.Page;
import com.jfeat.am.common.constant.tips.ErrorTip;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.common.exception.BusinessException;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.services.crud.service.TagManageService;
import com.jfeat.am.module.wegou.services.patch.TagPatchService;
import com.jfeat.am.module.wegou.services.persistence.model.TagManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 店铺标签表 api
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-03
 */
@Api("料理店铺标签管理")
@RestController
@RequestMapping("/api/tag")
public class TagManageEndpoint extends BaseController {

    @Resource
    TagManageService tagManageService;

    @Resource
    TagPatchService tagPatchService;


    @ApiOperation(value = "新添加标签", response = TagManage.class)
    @PostMapping
    public Tip createTag(@RequestBody TagManage entity) {
        try {
            tagManageService.createGroup(entity);
        } catch (Exception e) {
            throw new BusinessException(4000, "名字重复");
        }
        return SuccessTip.create(entity);
    }

    @ApiOperation(value = "获取标签信息", response = TagManage.class)
    @GetMapping("/{id}")
    public Tip getTag(@PathVariable Long id) {
        return SuccessTip.create(tagManageService.retrieveGroup(id));
    }


    @ApiOperation(value = "修改标签信息", response = TagManage.class)
    @PutMapping("/{id}")
    public Tip updateTag(@PathVariable Long id, @RequestBody TagManage entity) {
        entity.setId(id);
        TagManage originTag = tagManageService.retrieveGroup(id);
        if ((originTag.getTagName()).compareTo(entity.getTagName()) != 0) {
            try {
                tagManageService.updateGroup(entity);
            } catch (Exception e) {
                throw new BusinessException(4000, "名字重复");
            }
        }
        return SuccessTip.create(tagManageService.updateGroup(entity));
    }

    @ApiOperation(value = "删除标签", response = TagManage.class)
    @DeleteMapping("/{id}")
    public Tip deleteTag(@PathVariable Long id) {
        if (tagPatchService.hasChildren(id)) {
            return ErrorTip.create(WegouBizException.CANNOT_DELETE_WITH_CHILDREN.getCode(),
                    WegouBizException.CANNOT_DELETE_WITH_CHILDREN.getMessage());
        }
        return SuccessTip.create(tagManageService.deleteGroup(id));
    }

    @ApiOperation(value = "获取标签信息列表", response = TagManage.class)
    @GetMapping
    public Tip queryTag(Page page,
                        @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                        @RequestParam(name = "tagName", required = false) String tagName,
                        @RequestParam(name = "zhTagName", required = false) String zhTagName) {
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page.setRecords(tagManageService.queryTagManage(page, tagName, zhTagName));
        return SuccessTip.create(page);
    }

}
